package com.real.fact.web.product.domain;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.real.fact.web.application.RealPaging;

public class ProductRepository extends HibernateDaoSupport {

  public void save(final Product product) {
    getSession().save(product);
  }
  
  @SuppressWarnings("unchecked")
  public Product findById(final long id) {
    Criteria criteria = getSession().createCriteria(Product.class);
    criteria.add(Restrictions.eq("id", id));
    List<Product> products = criteria.list();
    if (products.size() > 0) {
      return products.get(0);
    }
    return null;
  }
  
  @SuppressWarnings("unchecked")
  public List<Product> findAll(RealPaging paging) {
    Criteria criteria = getSession().createCriteria(Product.class);
    paging.setRealTotalPageNumber(criteria.list().size());
    criteria.setFirstResult(paging.getPageNumber() * paging.getPageSize());
    criteria.setMaxResults(paging.getPageSize());
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    return criteria.list();
  }
  
  @SuppressWarnings("unchecked")
  public List<Product> findByName(final String name, RealPaging paging) {
    Criteria criteria = getSession().createCriteria(Product.class);
    criteria.add(Restrictions.ilike("name", name, MatchMode.ANYWHERE));
    criteria.add(Restrictions.eq("active", true));
    paging.setRealTotalPageNumber(criteria.list().size());
    criteria.setFirstResult(paging.getPageNumber() * paging.getPageSize());
    criteria.setMaxResults(paging.getPageSize());
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    return criteria.list();
  }

  public void remove(final long id) {
    Product product = findById(id);
    getSession().delete(product);
  }

  @SuppressWarnings("unchecked")
  public List<Product> filter(RealPaging paging, ProductFilter filter) {
    Criteria criteria = getSession().createCriteria(Product.class);
    criteria.add(Restrictions.ilike(filter.getFieldName().toLowerCase(),
        filter.getFieldValue(), MatchMode.ANYWHERE));
    if (filter.isActive()) {
      criteria.add(Restrictions.eq("active", true));
    }
    if (paging != null) {
      paging.setRealTotalPageNumber(criteria.list().size());
      criteria.setFirstResult(paging.getPageNumber() * paging.getPageSize());
      criteria.setMaxResults(paging.getPageSize());
    }
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    return criteria.list();
  }

  @SuppressWarnings("unchecked")
  public Product findByCode(String code) {
    Criteria criteria = getSession().createCriteria(Product.class);
    criteria.add(Restrictions.eq("code", code));
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    List<Product> list = criteria.list();
    if (list.size() > 0) {
      return list.get(0);
    }
    return null;
  }
  
}
